package com.naihe.anderson.sms.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.naihe.anderson.sms.dbhelper.DbReadWriteLock;
import com.naihe.anderson.sms.dbhelper.NoteDBHelper;
import com.naihe.anderson.sms.entity.Note;
import com.naihe.anderson.sms.util.FileUtil;

public class NoteDao {
	private NoteDBHelper dbHelper;
	private SQLiteDatabase db;
	private static DbReadWriteLock dbReadWriteLock = new DbReadWriteLock();

	public NoteDao(Context context) {
		dbHelper = new NoteDBHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		if (dbHelper != null) {
			dbHelper.close();
		}
	}	
	
	/**
	 * 
	 * @param list
	 */
	public void deleteNoteEntity(List<Note> list){
		try {
			dbReadWriteLock.writeLock();
			db.beginTransaction();
			 for(Note note : list){
				 db.delete(NoteDBHelper.NOTE_TABLE_NAME, Note._ID + " = ? ", new String[]{note.id + ""});
			 }
			db.setTransactionSuccessful();
		} catch (Exception e) {
			FileUtil.addLog("NoteDao::deleteNoteEntity", NoteDao.class, "NoteDao", e);
		} finally {
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 * @param list
	 */
	public void deleteNoteEntity(Note delNote){
		try {
			dbReadWriteLock.writeLock();
			db.delete(NoteDBHelper.NOTE_TABLE_NAME, Note._ID + " = ? ", new String[]{delNote.id + ""});
		} catch (Exception e) {
			FileUtil.addLog("NoteDao::deleteNoteEntity", NoteDao.class, "NoteDao", e);
		} finally {
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public void setNotePasswd(long Id, String passwd){
		try {
			 dbReadWriteLock.writeLock();
			 String sql="update " + NoteDBHelper.NOTE_TABLE_NAME + " set " + Note.CIPHER + "='" + passwd + "' where " + Note._ID + "=" + Id;
			 db.execSQL(sql);
		}catch (Exception e) {
			FileUtil.addLog("NoteDao::setNotePasswd", NoteDao.class, "NoteDao", e);
		}finally{
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 * @param list
	 */
	public void insertOrUpdateNoteEntity(List<Note> list){
		try {
			 dbReadWriteLock.writeLock();
			 db.beginTransaction();
			 ContentValues values = new ContentValues();
			 for(Note note : list){
				 values.put(Note._ID, note.id);
				 values.put(Note.BODY, note.body);
				 values.put(Note.CIPHER, note.cipher);
				 values.put(Note.CREATE_TIME, note.createTime);
				 values.put(Note.TITLE, note.title);
				 values.put(Note.PICPATH, note.picpath);
				 
				 db.replace(NoteDBHelper.NOTE_TABLE_NAME, null, values);
				 values.clear();
			 }
			 values = null;
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("NoteDao::insertOrUpdateNoteEntity", NoteDao.class, "NoteDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 */
	public void insertOrUpdateNoteEntity(Note note){
		try {
			 dbReadWriteLock.writeLock();
			 ContentValues values = new ContentValues();
			 values.put(Note._ID, note.id);
			 values.put(Note.BODY, note.body);
			 values.put(Note.CIPHER, note.cipher);
			 values.put(Note.CREATE_TIME, note.createTime);
			 values.put(Note.TITLE, note.title);
			 values.put(Note.PICPATH, note.picpath);
			 
			 db.replace(NoteDBHelper.NOTE_TABLE_NAME, null, values);
			 values = null;
		}catch (Exception e) {
			FileUtil.addLog("NoteDao::insertOrUpdateNoteEntity", NoteDao.class, "NoteDao", e);
		}finally{
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public List<Note> queryAllNoteList() {
		 List<Note> list = new ArrayList<Note>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(NoteDBHelper.NOTE_TABLE_NAME, NoteDBHelper.note_projection, null, null, null, null, Note.CREATE_TIME + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					Note entity = new Note();
					entity.id = smscur.getLong(smscur.getColumnIndex(Note._ID));
					entity.createTime = smscur.getLong(smscur.getColumnIndex(Note.CREATE_TIME));
					entity.body = smscur.getString(smscur.getColumnIndex(Note.BODY));
					entity.cipher = smscur.getString(smscur.getColumnIndex(Note.CIPHER));
					entity.title = smscur.getString(smscur.getColumnIndex(Note.TITLE));
					entity.picpath = smscur.getString(smscur.getColumnIndex(Note.PICPATH));
					list.add(entity);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("NoteDao::queryAllNoteList", NoteDao.class, "NoteDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return list;
	 }
}
